package com.yodawnla.katana.scene;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.util.MathUtils;

import com.yodawnla.katana.ClientHandler;
import com.yodawnla.katana.GameMsgHandler;
import com.yodawnla.katana.NameCreator;
import com.yodawnla.katana.Player;
import com.yodawnla.katana.R;
import com.yodawnla.katana.Hud.PushHud;
import com.yodawnla.katana.Hud.ResultHud;
import com.yodawnla.katana.system.Cmd;
import com.yodawnla.katana.system.SE;
import com.yodawnla.katana.system.Tex;
import com.yodawnla.katana.system.Values;
import com.yodawnla.lib.network.YoClient;
import com.yodawnla.lib.util.YoInt;
import com.yodawnla.lib.util.tool.YoTimer;
import com.yodawnla.lib.util.tool.YoTimer.TimerEvent;
import com.yodawnla.lib.util.widget.YoButton;

public class SingleScene extends GameScene
{
	int mTitleID;
	YoInt mEnemyAmount = new YoInt(1);
	YoInt mEnemyAmountTemp = new YoInt(0);
	int mLv = 0;

	YoClient mClient;
	
	@Override
	public void setWaitTime(float time)
	{
		// 全神貫注..
		playSound(SE.StartMatch);
		mEnemyStand.setVisible(true);

		mPrepare.setVisible(true);
		mPrepare.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1f, 1, 1), new AlphaModifier(1f, 1, 0)));

		mTaiko1.setVisible(false);
		mTaiko2.setVisible(false);

		if (mLv == 0) // 教學
		{
			YoButton tut = new YoButton(this, 0, 0, 800, 480, getTexture(Tex.Tip))
			{
				public void onClickedEvent()
				{
					this.setVisible(false);
					playSound(SE.Pull);
					mPrepare.setVisible(false);
					PushHud.getInstance().initPush();
					PushHud.getInstance().show();
				}
			};
			this.attachChild(tut);
		}
		else
		{
			mWaitTimer = new YoTimer(time)
			{
				public void onTimePassed() // MEMO 時間到，開始拔刀判定
				{
					playSound(SE.Pull);
					mPrepare.setVisible(false);
					PushHud.getInstance().initPush();
					PushHud.getInstance().show();

					this.stop();
					mWaitTimer = null;
				}
			};
			mWaitTimer.start();
		}
	}

	public void setEnemyLv(int lv)
	{
		mLv = lv;
		// 敵人強度
		switch (mLv)
		{
		case 0:
			mEnemyAmount.set(1);
			break;
		case 1:
			mEnemyAmount.set(2);
			break;
		case 2:
			mEnemyAmount.set(2);
			break;
		case 3:
			mEnemyAmount.set(3);
			break;
		case 4:
			mEnemyAmount.set(3);
			break;
		case 5:
			mEnemyAmount.set(3);
			break;
		case 6:
			mEnemyAmount.set(4);
			break;
		case 7:
			mEnemyAmount.set(4);
			break;
		case 8:
			mEnemyAmount.set(4);
			break;
		case 9:
			mEnemyAmount.set(5);
			break;
		}
	}

	private void _setAILv()
	{
		// 敵人強度
		switch (mLv)
		{
		case 0:
			mOpponentScore = MathUtils.random(1, 1000);
			break;
		case 1:
			mOpponentScore = MathUtils.random(4000, 8000);
			break;
		case 2:
			mOpponentScore = MathUtils.random(8000, 12000);
			break;
		case 3:
			mOpponentScore = MathUtils.random(12000, 16000);
			break;
		case 4:
			mOpponentScore = MathUtils.random(16000, 20000);
			break;
		case 5:
			mOpponentScore = MathUtils.random(20000, 24000);
			break;
		case 6:
			mOpponentScore = MathUtils.random(24000, 28000);
			break;
		case 7:
			mOpponentScore = MathUtils.random(28000, 32000);
			break;
		case 8:
			mOpponentScore = MathUtils.random(32000, 36000);
			break;
		case 9:
			mOpponentScore = MathUtils.random(36000, 40000);
			break;
		}
	}

	@Override
	public void onEnterScene()
	{
		Player.getInstance().setCurrentScene(this);
		super.onEnterScene();
		
		if (mClient == null)
		{
			mClient = new YoClient(Values.PORT, new ClientHandler()
			{
				@Override
				public void onConnected()
				{
					mClient.sendMesg(Cmd.PlayerInfo + "#" + mPlayer.getInfoString());
				}
				
				@Override
				public void onGetResult(String result)
				{
					if (mStartJudge && result.equals(Cmd.Challenge))
					{
						// TODO 顯示接受挑戰hud
						ResultHud.getInstance().showChallenger();
					}
				}
			});
		}
		
		mEnemyAmountTemp.set(0);
		mSingleMode = true;

		_checkEnd();

		

		mLeafEffect.resume();

		setMoveGrass(true);

		createTempTimer(5, new TimerEvent()
		{
			public void onTimePassed()
			{
				float time = MathUtils.random(1.5f, 4);
				_createEnemyInfo();
				GameMsgHandler.getInstance().handlerMsg(SingleScene.this, Cmd.WaitTime + "#" + time);
				_setAILv();
				this.stop();
			}
		}).start();
	}

	public int getLevel()
	{
		return mLv;
	}

	private void _checkEnd()
	{
		mEnemyAmountTemp.add(1);
		if (mEnemyAmountTemp.get() >= mEnemyAmount.get())
			ResultHud.getInstance().setIsEnd(true, mLv, 0);
		else
		{
			ResultHud.getInstance().setIsEnd(false, mLv, mEnemyAmount.get() - mEnemyAmountTemp.get());
		}
	}

	public void nextStage()
	{
		resetScene();
		_checkEnd();
		createTempTimer(MathUtils.random(1.5f, 4f), new TimerEvent()
		{
			public void onTimePassed()
			{
				float time = MathUtils.random(1.5f, 4);
				_createEnemyInfo();
				GameMsgHandler.getInstance().handlerMsg(SingleScene.this, Cmd.WaitTime + "#" + time);
				_setAILv();

				this.stop();
			}
		}).start();
	}

	@Override
	public void onExitScene()
	{
		super.onExitScene();
		if (mClient != null)
			mClient.closeConnection();
		mClient = null;
	}

	@Override
	public void onFinishMatch()
	{
		this.mClient.sendMesg(Cmd.Upload + "#" + -1 + "@" + mPlayer.getSpTemp());
	}

	private void _createEnemyInfo()
	{
		int value = mPlayer.getMaxKillTemp() / 5;
		setEnemyTitle(MathUtils.random(0, 3 + value));

		mEnemyName = NameCreator.getInstance().getRandomName();
		mEnemySkillText = getRandomSkillName();
		mEnemyGreetText = getRandomGreeText();
		mEnemyWinText = getRandomWinText();
		mEnemyLoseText = getRandomLoseText();

		if (MathUtils.random(0, 4) == 2) // 有機會出現其他臉圖
			mEnemyFaceType = MathUtils.random(0, Values.MAX_CHAR_PAGE - 1);
		else
		{
			mEnemyFaceType = 0; // 一般雜魚
		}
//		mEnemyFaceType = 2;
	}

	public String getRandomSkillName()
	{
		String name1 = "";
		String name2 = "";
		String name3 = "";

		int randValue = MathUtils.random(0, 72);
		switch (randValue)
		{
		case 0:
			name1 = "飛龍";
			break;
		case 1:
			name1 = "百八";
			break;
		case 2:
			name1 = "鬼";
			break;
		case 3:
			name1 = "虎狩";
			break;
		case 4:
			name1 = "夜叉";
			break;
		case 5:
			name1 = "天馬";
			break;
		case 6:
			name1 = "斯巴達";
			break;
		case 7:
			name1 = "炎獄";
			break;
		case 8:
			name1 = "六道";
			break;
		case 9:
			name1 = "旋風";
			break;
		case 10:
			name1 = "真．";
			break;
		case 11:
			name1 = "居合";
			break;
		case 12:
			name1 = "北斗";
			break;
		case 13:
			name1 = "風魔";
			break;
		case 14:
			name1 = "御劍流";
			break;
		case 15:
			name1 = "阿修羅";
			break;
		case 16:
			name1 = "八咫";
			break;
		case 17:
			name1 = "龜派";
			break;
		case 18:
			name1 = "石破";
			break;
		case 19:
			name1 = "邪王";
			break;
		case 20:
			name1 = "死亡";
			break;
		case 21:
			name1 = "千年";
			break;
		case 22:
			name1 = "七星";
			break;
		case 23:
			name1 = "冥界";
			break;
		case 24:
			name1 = "二刀流。";
			break;
		case 25:
			name1 = "三刀流。";
			break;
		case 26:
			name1 = "太陽";
			break;
		case 27:
			name1 = "三千";
			break;
		case 28:
			name1 = "巨型";
			break;
		case 29:
			name1 = "一刀流。";
			break;
		case 30:
			name1 = "普通的";
			break;
		case 31:
			name1 = "超級";
			break;
		case 32:
			name1 = "尿遁．";
			break;
		case 33:
			name1 = "火遁．";
			break;
		case 34:
			name1 = "水遁．";
			break;
		case 35:
			name1 = "雷遁．";
			break;
		case 36:
			name1 = "土遁．";
			break;
		case 37:
			name1 = "風遁．";
			break;
		case 38:
			name1 = "木遁．";
			break;
		case 39:
			name1 = "會心";
			break;
		case 40:
			name1 = "神風流";
			break;
		case 41:
			name1 = "超音速";
			break;
		case 42:
			name1 = "光速";
			break;
		case 43:
			name1 = "黃金";
			break;
		case 44:
			name1 = "地獄";
			break;
		case 45:
			name1 = "假．";
			break;
		case 46:
			name1 = "百萬";
			break;
		case 47:
			name1 = "千萬";
			break;
		case 48:
			name1 = "次元";
			break;
		case 49:
			name1 = "幻影";
			break;
		case 50:
			name1 = "神奇";
			break;
		case 51:
			name1 = "飛燕";
			break;
		case 52:
			name1 = "無我流．";
			break;
		case 53:
			name1 = "零式";
			break;
		case 54:
			name1 = "月亮";
			break;
		case 55:
			name1 = "多重";
			break;
		case 56:
			name1 = "烈焰";
			break;
		case 57:
			name1 = "九連發";
			break;
		case 58:
			name1 = "六式．";
			break;
		case 59:
			name1 = "惡魔";
			break;
		case 60:
			name1 = "閃光";
			break;
		case 61:
			name1 = "千本";
			break;
		case 62:
			name1 = "百式";
			break;
		case 63:
			name1 = "霸王";
			break;
		case 64:
			name1 = "特級";
			break;
		case 65:
			name1 = "暴走";
			break;
		case 66:
			name1 = "幻想";
			break;
		case 67:
			name1 = "真空．";
			break;
		case 68:
			name1 = "滅．";
			break;
		case 69:
			name1 = "極光";
			break;
		case 70:
			name1 = "奧義．";
			break;
		case 71:
			name1 = "最終";
			break;
		case 72:
			name1 = "必殺．";
			break;
		}

		randValue = MathUtils.random(0, 90);
		switch (randValue)
		{
		case 0:
			name2 = "";
			break;
		case 1:
			name2 = "沖擊";
			break;
		case 2:
			name2 = "旋風";
			break;
		case 3:
			name2 = "噴射";
			break;
		case 4:
			name2 = "世界";
			break;
		case 5:
			name2 = "電磁";
			break;
		case 6:
			name2 = "天驚";
			break;
		case 7:
			name2 = "黑龍";
			break;
		case 8:
			name2 = "氣功";
			break;
		case 9:
			name2 = "霸王";
			break;
		case 10:
			name2 = "流星";
			break;
		case 11:
			name2 = "氣";
			break;
		case 12:
			name2 = "修羅";
			break;
		case 13:
			name2 = "羅生";
			break;
		case 14:
			name2 = "龍";
			break;
		case 15:
			name2 = "獅子";
			break;
		case 16:
			name2 = "之";
			break;
		case 17:
			name2 = "破軍";
			break;
		case 18:
			name2 = "七星";
			break;
		case 19:
			name2 = "爆菊";
			break;
		case 20:
			name2 = "螺旋";
			break;
		case 21:
			name2 = "火焰";
			break;
		case 22:
			name2 = "風暴";
			break;
		case 23:
			name2 = "閃";
			break;
		case 24:
			name2 = "冥";
			break;
		case 25:
			name2 = "鏡反";
			break;
		case 26:
			name2 = "碎腦";
			break;
		case 27:
			name2 = "無敵";
			break;
		case 28:
			name2 = "秒殺";
			break;
		case 29:
			name2 = "奪魂";
			break;
		case 30:
			name2 = "魔";
			break;
		case 31:
			name2 = "神";
			break;
		case 32:
			name2 = "煉獄";
			break;
		case 33:
			name2 = "究極";
			break;
		case 34:
			name2 = "衝擊";
			break;
		case 35:
			name2 = "虎";
			break;
		case 36:
			name2 = "束縛";
			break;
		case 37:
			name2 = "火箭";
			break;
		case 38:
			name2 = "轉生";
			break;
		case 39:
			name2 = "水月";
			break;
		case 40:
			name2 = "爆裂";
			break;
		case 41:
			name2 = "豪火";
			break;
		case 42:
			name2 = "火龍";
			break;
		case 43:
			name2 = "鬼燈";
			break;
		case 44:
			name2 = "水龍";
			break;
		case 45:
			name2 = "水牢";
			break;
		case 46:
			name2 = "水鮫";
			break;
		case 47:
			name2 = "神風";
			break;
		case 48:
			name2 = "砂塵";
			break;
		case 49:
			name2 = "真空";
			break;
		case 50:
			name2 = "烈風";
			break;
		case 51:
			name2 = "裂土";
			break;
		case 52:
			name2 = "黃泉";
			break;
		case 53:
			name2 = "土龍";
			break;
		case 54:
			name2 = "風龍";
			break;
		case 55:
			name2 = "加重";
			break;
		case 56:
			name2 = "地動";
			break;
		case 57:
			name2 = "雷切";
			break;
		case 58:
			name2 = "千鳥";
			break;
		case 59:
			name2 = "雷龍";
			break;
		case 60:
			name2 = "麒麟";
			break;
		case 61:
			name2 = "電光";
			break;
		case 62:
			name2 = "時空";
			break;
		case 63:
			name2 = "空間";
			break;
		case 64:
			name2 = "逆轉";
			break;
		case 65:
			name2 = "裂骨";
			break;
		case 66:
			name2 = "回天";
			break;
		case 67:
			name2 = "琉璃";
			break;
		case 68:
			name2 = "無限";
			break;
		case 69:
			name2 = "月讀";
			break;
		case 70:
			name2 = "咒印";
			break;
		case 71:
			name2 = "毒龍";
			break;
		case 72:
			name2 = "天衝";
			break;
		case 73:
			name2 = "雷霆";
			break;
		case 74:
			name2 = "光龍";
			break;
		case 75:
			name2 = "暗龍";
			break;
		case 76:
			name2 = "煩惱";
			break;
		case 77:
			name2 = "波動";
			break;
		case 78:
			name2 = "冰凍";
			break;
		case 79:
			name2 = "吹雪";
			break;
		case 80:
			name2 = "攻城";
			break;
		case 81:
			name2 = "炸裂";
			break;
		case 82:
			name2 = "奈落";
			break;
		case 83:
			name2 = "毒雲";
			break;
		case 84:
			name2 = "審判";
			break;
		case 85:
			name2 = "赤魔";
			break;
		case 86:
			name2 = "鐘擺";
			break;
		case 87:
			name2 = "解體";
			break;
		case 88:
			name2 = "萬雷";
			break;
		case 89:
			name2 = "千億伏特";
			break;
		case 90:
			name2 = "金剛";
			break;
		}

		randValue = MathUtils.random(0, 35);
		switch (randValue)
		{
		case 0:
			name3 = "斬";
			break;
		case 1:
			name3 = "閃";
			break;
		case 2:
			name3 = "破";
			break;
		case 3:
			name3 = "拳";
			break;
		case 4:
			name3 = "砍";
			break;
		case 5:
			name3 = "刀";
			break;
		case 6:
			name3 = "波";
			break;
		case 7:
			name3 = "球";
			break;
		case 8:
			name3 = "踢";
			break;
		case 9:
			name3 = "擊";
			break;
		case 10:
			name3 = "砲";
			break;
		case 11:
			name3 = "刃";
			break;
		case 12:
			name3 = "舞";
			break;
		case 13:
			name3 = "絕";
			break;
		case 14:
			name3 = "之術";
			break;
		case 15:
			name3 = "亂打";
			break;
		case 16:
			name3 = "打";
			break;
		case 17:
			name3 = "槍";
			break;
		case 18:
			name3 = "鞭";
			break;
		case 19:
			name3 = "火箭";
			break;
		case 20:
			name3 = "彈";
			break;
		case 21:
			name3 = "電影彈";
			break;
		case 22:
			name3 = "刺";
			break;
		case 23:
			name3 = "突";
			break;
		case 24:
			name3 = "掌";
			break;
		case 25:
			name3 = "劍";
			break;
		case 26:
			name3 = "斧";
			break;
		case 27:
			name3 = "腿";
			break;
		case 28:
			name3 = "爪";
			break;
		case 29:
			name3 = "吼";
			break;
		case 30:
			name3 = "加農砲";
			break;
		case 31:
			name3 = "激光";
			break;
		case 32:
			name3 = "改";
			break;
		case 33:
			name3 = "大滿貫";
			break;
		case 34:
			name3 = "鑽";
			break;
		case 35:
			name3 = "牙";
			break;
		}

		return name1 + name2 + name3;
	}

	public String getRandomGreeText()
	{
		String greeText = "";
		int randValue = MathUtils.random(0, 43);

		greeText = getRString(R.string.greet0 + randValue);
		return greeText;
	}

	public String getRandomWinText()
	{
		String winText = "";
		int randValue = MathUtils.random(0, 35);

		winText = getRString(R.string.win0 + randValue);
		return winText;
	}

	public String getRandomLoseText()
	{
		String loseText = "";
		int randValue = MathUtils.random(0, 37);
		loseText = getRString(R.string.lose0 + randValue);
		return loseText;
	}

	@Override
	public void exitBattle()
	{
		toScene("StageScene");
	}
}